package com.manli.api.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.manli.api.enums.ResultEnums;
import com.manli.api.pojo.Result;
import com.manli.api.pojo.SessionInfo;
import com.manli.api.service.wechat.WechatShopService;
import com.manli.api.util.ResultUtil;
import com.manli.api.util.SessionInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.manli.api.base.annotation.Permissions;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dto.UserLinkUnionid;
import com.manli.api.service.UserLinkUnionidService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author manli
 * @email 
 * @date 2020-07-02 12:19:13
 */
@Api(value="/userlinkunionid", tags="")
@RestController
@RequestMapping("api/userlinkunionid")
public class UserLinkUnionidController {
    @Autowired
    private UserLinkUnionidService userLinkUnionidService;
    @Autowired
    WechatShopService wechatShopService;
    @Autowired
    SessionInfoUtils sessionInfoUtils;

    @ApiOperation(value = "绑定",notes = "")
    @PostMapping("/bind")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result saveOrUpdate(@RequestBody UserLinkUnionid userLinkUnionid){
        UserLinkUnionid data = userLinkUnionidService.getSingleByUnionid(userLinkUnionid.getUnionid(),1);
        UserLinkUnionid dataOther = userLinkUnionidService.getByUserId(userLinkUnionid.getUserId(),1);
        if (data != null || dataOther != null){
            if(!data.getUserId().equals(userLinkUnionid.getUserId())){
                return ResultUtil.error(ResultEnums.USER_UNIONID_IS_EXIT.getCode(),ResultEnums.USER_UNIONID_IS_EXIT.getMsg());
            }
            if(!dataOther.getUnionid().equals(userLinkUnionid.getUnionid())){
                return ResultUtil.error(ResultEnums.USER_UNIONID_IS_EXIT.getCode(),ResultEnums.USER_UNIONID_IS_EXIT.getMsg());
            }
        }
        userLinkUnionid.setStatus(1);
        userLinkUnionidService.save(userLinkUnionid);
        return ResultUtil.success("微信和管理系统账号绑定成功");
    }

    @ApiOperation(value = "后台账号解绑微信",notes = "")
    @PostMapping("/disbind")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result disbind(@RequestBody UserLinkUnionid userLinkUnionid){
        UserLinkUnionid data = userLinkUnionidService.getSingleByUnionid(userLinkUnionid.getUnionid(),1);
        UserLinkUnionid dataOther = userLinkUnionidService.getByUserId(userLinkUnionid.getUserId(),1);
        if (data != null || dataOther != null){
            if(!data.getUserId().equals(userLinkUnionid.getUserId())){
                return ResultUtil.error(ResultEnums.USER_UNIONID_IS_EXIT.getCode(),ResultEnums.USER_UNIONID_IS_EXIT.getMsg());
            }
            if(!dataOther.getUnionid().equals(userLinkUnionid.getUnionid())){
                return ResultUtil.error(ResultEnums.USER_UNIONID_IS_EXIT.getCode(),ResultEnums.USER_UNIONID_IS_EXIT.getMsg());
            }
            List<Integer> removeIds = new ArrayList<>();
            removeIds.add(data.getUserLinkUnionidId());
            userLinkUnionidService.removeByIds(removeIds);
            return ResultUtil.success("微信和管理系统账号解绑成功");
        }
        return ResultUtil.error(ResultEnums.USER_UNIONID_IS_EXIT.getCode(),ResultEnums.USER_UNIONID_IS_EXIT.getMsg());

    }

    @ApiOperation(value = "轮训判断微信和后台账号是否绑定",notes = "")
    @PostMapping("/checkBindUnionLinkUser")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result checkBindUnionLinkUserCode(HttpServletRequest request){
        SessionInfo sessionInfo = sessionInfoUtils.getSessionInfoByAuth(request);
        UserLinkUnionid userLinkUnionid = userLinkUnionidService.getByUserId(sessionInfo.getUserId(),1);
        return ResultUtil.success(userLinkUnionid);
    }

    @ApiOperation(value = "微信账号绑定获取code",notes = "微信账号绑定获取code")
    @PostMapping("/getUnionLinkUserCode")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result getUnionLinkUserCode(HttpServletRequest request){
        return wechatShopService.getUnionLinkUserCode(request);
    }

    /**
     * 列表
     */
    @ApiOperation(value = "列表",notes = "")
    @PostMapping("/list")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result list(@RequestBody QueryParams params){
        return userLinkUnionidService.queryPage(params);
    }

    /**
     * 全部数据信息
     */
    @ApiOperation(value = "全部数据信息",notes = "")
    @PostMapping("/all")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result all(){
        return userLinkUnionidService.queryAll();
    }

    /**
     * 单条信息
     */
    @ApiOperation(value = "单条信息",notes = "单条信息")
    @GetMapping("/info/{userLinkUnionidId}")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result info(@PathVariable("userLinkUnionidId") Integer userLinkUnionidId){
        return userLinkUnionidService.getById(userLinkUnionidId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存",notes = "保存")
    @PostMapping("/save")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result save(@RequestBody UserLinkUnionid userLinkUnionid){
        return userLinkUnionidService.save(userLinkUnionid);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改",notes = "修改")
    @PostMapping("/update")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result update(@RequestBody UserLinkUnionid userLinkUnionid){
        return userLinkUnionidService.updateById(userLinkUnionid);
    }


    /**
     * 删除多条数据
     */
    @ApiOperation(value = "删除",notes = "删除")
    @ApiImplicitParam(name = "userLinkUnionidIds", value = "id数组,json字符串",dataType = "id数组,json字符串", required = true)
    @PostMapping("/deleteIds")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result deleteIds(@RequestBody Integer[] userLinkUnionidIds){
        return userLinkUnionidService.removeByIds(Arrays.asList(userLinkUnionidIds));
    }


}
